package com.alex.springboot02web.controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ParameterTestController {

    @GetMapping("/test/param/car/{id}/owner/{name}")
    public Map<String, Object> getCar(@PathVariable("id") Integer id,
                                      @PathVariable("name") String owner,
                                      @PathVariable Map<String, String> map,
                                      @RequestHeader("user-agent") String userAgent,
                                      @RequestHeader("cookie") String cookies,
                                      @RequestHeader Map<String, String> header,
                                      @RequestParam("age") Integer age,
                                      @RequestParam("languages") List<String> languages,
                                      @RequestParam Map<String, String> params,
                                      @CookieValue("JSESSIONID") String JSESSIONID,
                                      @CookieValue("JSESSIONID") Cookie JSESSIONID___Cookie) {

        HashMap<String, Object> carMap = new HashMap<>();
//        carMap.put("id", id);
//        carMap.put("owner", owner);
//        carMap.put("color", "blue");
//        carMap.put("pv", map);
//        carMap.put("userAgent", userAgent);
//        carMap.put("header", header);
//        carMap.put("age", age);
//        carMap.put("languages", languages);
//        carMap.put("params", params); // 实际获取请求参数不要使用 map, 因为value类型都为string需要自己再次转换类型, 而且无法获取列表
        carMap.put("cookie", cookies);
        carMap.put("JSESSIONID", JSESSIONID);
        carMap.put("JSESSIONID_Cookie", JSESSIONID___Cookie);

        return carMap;
    }

    @PostMapping("/test/param/user")
    public Map getUser(
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            @RequestBody String content) {
        HashMap<String, Object> body = new HashMap<>();
        body.put("username", username);
        body.put("password", password);
        body.put("content", content);

        return body;
    }
}
